package com.lebang.medicalInquiry.controller;


import com.lebang.medicalInquiry.constant.ResponseEnum;
import com.lebang.medicalInquiry.model.po.Article;
import com.lebang.medicalInquiry.model.po.ArticleClassify;
import com.lebang.medicalInquiry.model.vo.PageVo;
import com.lebang.medicalInquiry.response.Callback;
import com.lebang.medicalInquiry.response.JsonData;
import com.lebang.medicalInquiry.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/article")
@Api(tags = "文章相关接口")
@RestController
@Validated
public class ArticleController {


    @Autowired
    private ArticleService service;

    @GetMapping("/")
    @ApiOperation("文章列表")
    public JsonData<PageVo<List<Article>>> listArticle(HttpServletRequest request, HttpServletResponse response,
                                                       @ApiParam(name = "keyWord", value = "标题关键词") @RequestParam(value = "keyWord", required = false) String keyWord,
                                                       @ApiParam(name = "state", value = "0 禁用 1 启用 -1 所有") @RequestParam(value = "state", required = false, defaultValue = "-1") Integer state,
                                                       @ApiParam(name = "classifyId", value = "分类id -1 所有") @RequestParam(value = "classifyId", required = false, defaultValue = "-1") Integer classifyId,
                                                       @ApiParam(name = "page", value = "页码") @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                       @ApiParam(name = "limit", value = "条数") @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

        PageVo<List<Article>> list = service.getList(keyWord, state, classifyId, page, limit);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), list);

    }

    @PostMapping("/")
    @ApiOperation("修改文章")
    public JsonData operator(HttpServletRequest request, HttpServletResponse response
            , @RequestBody Article po

    ) {


        service.operator(po);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());

    }


    @DeleteMapping("/{id}")
    @ApiOperation("删除文章")
    public JsonData remove(HttpServletRequest request, HttpServletResponse response
            , @ApiParam(name = "id", value = "id", required = true) @PathVariable Integer id

    ) {

        service.remove(id);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());

    }


    @GetMapping("/classify")
    @ApiOperation("查询文章分类标签")
    public JsonData<PageVo<List<ArticleClassify>>> listArticleClassify(HttpServletRequest request, HttpServletResponse response,
                                                                       @ApiParam(name = "keyWord", value = "关键词") @RequestParam(value = "keyWord", required = false) String keyWord,
                                                                       @ApiParam(name = "state", value = "0 禁用 1 启用 -1 所有") @RequestParam(value = "keyWord", required = false, defaultValue = "-1") Integer state,
                                                                       @ApiParam(name = "page", value = "页码") @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                       @ApiParam(name = "limit", value = "条数") @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

        PageVo<List<ArticleClassify>> list = service.listArticleClassify(keyWord, state, page, limit);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), list);

    }


    @PostMapping("/classify")
    @ApiOperation("修改文章分类标签")
    public JsonData operatorClassify(HttpServletRequest request, HttpServletResponse response
            , @RequestBody ArticleClassify po

    ) {


        service.operatorClassify(po);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());

    }

    @DeleteMapping("/classify/{id}")
    @ApiOperation("删除文章分类标签")
    public JsonData removeClassify(HttpServletRequest request, HttpServletResponse response
            , @ApiParam(name = "id", value = "id", required = true) @PathVariable Integer id

    ) {

        service.removeClassify(id);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());

    }

}